package ucadminlogic

import (
	"context"
	"encoding/json"
	"fmt"
	"go.uber.org/zap"
	ucsyslogic "navi-mumbai/app/rpc/usercenter/internal/logic/ucsys"
	"navi-mumbai/app/rpc/usercenter/internal/svc"
	"navi-mumbai/app/rpc/usercenter/pb"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/key/userkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReplyFeedbackLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReplyFeedbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReplyFeedbackLogic {
	return &ReplyFeedbackLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 回复用户反馈
func (l *ReplyFeedbackLogic) ReplyFeedback(in *pb.ReplyFeedbackReq) (*pb.ReplyFeedbackResp, error) {
	defer utils.RecoverPanic()

	err := l.svcCtx.UserFeedbackModel.Reply(l.ctx, in.Id, in.AdminUid, userkey.FeedbackStateHandled, in.Reply, in.ReplyAttachments)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	//
	//l.after(in.Uid, in.Reply, in.ReplyAttachments)
	// 发送rpc消息，到通知列表中去
	l.afterInsertNotifyMsg(in.Uid, in.Reply, in.ReplyAttachments)
	return &pb.ReplyFeedbackResp{}, nil
}

func (l *ReplyFeedbackLogic) afterInsertNotifyMsg(uid int64, reply string, imgs string) {
	if uid <= 0 {
		return
	}
	title := userkey.UserNotifyMsgTitleFeedbackReply10EnIn
	var msgType int64 = userkey.UserNotifyMsgTypeFeedbackReply10
	msgContent := userkey.UserNotifyMsgContent1{
		Text:     fmt.Sprintf(userkey.UserNotifyMsgContentFeedbackReply10EnIn, reply),
		Url:      "",
		PagePath: "",
	}
	if len(imgs) > 0 {
		msgContent.Imgs = strings.Split(imgs, dbkey.Separator)
	}
	var cBuf []byte
	cBuf, err := json.Marshal(msgContent)
	if err != nil {
		l.Logger.Errorf("UpdateDepositResultLogic after json.Marshal(msgContent) err:%v", err)
		return
	}
	in := &pb.AddUserNotifyMsgReq{
		Uid:        uid,
		Title:      title,
		MsgType:    msgType,
		MsgContent: string(cBuf),
	}
	sendMsg := ucsyslogic.NewAddUserNotifyMsgLogic(l.ctx, l.svcCtx)
	_, err = sendMsg.AddUserNotifyMsg(in)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
	}
	l.Logger.Errorf("afterInsertNotifyMsg sucess.")
	return
}

/*func (l *ReplyFeedbackLogic) after(uid int64, reply string, imgs string) {
	if uid <= 0 {
		return
	}

	kqMsg := kqmsg.KqAddUserNotifyMsg{
		Uid:     uid,
		Title:   userkey.UserNotifyMsgTitleFeedbackReply10EnIn,
		MsgType: userkey.UserNotifyMsgTypeFeedbackReply10,
	}

	msgContent := userkey.UserNotifyMsgContent1{
		Text:     fmt.Sprintf(userkey.UserNotifyMsgContentFeedbackReply10EnIn, reply),
		Url:      "",
		PagePath: "",
	}
	if len(imgs) > 0 {
		msgContent.Imgs = strings.Split(imgs, dbkey.Separator)
	}
	var cBuf []byte
	cBuf, err := json.Marshal(msgContent)
	if err != nil {
		l.Logger.Errorf("UpdateDepositResultLogic after json.Marshal(msgContent) err:%v", err)
		return
	}
	kqMsg.MsgContent = string(cBuf)

	var kqMsgBuf []byte
	kqMsgBuf, err = json.Marshal(kqMsg)
	if err != nil {
		l.Logger.Errorf("UpdateDepositResultLogic after json.Marshal(kqMsg) err:%v", err)
		return
	}

	err = l.svcCtx.KqAddUserNotifyMsgClient.Push(string(kqMsgBuf))
	if err != nil {
		l.Logger.Errorf("UpdateDepositResultLogic after Push err:%v", err)
		return
	}
	return
}

*/
